$(document).ready(function(){
	/**
	 * 身份证号码
	 */
	var checkCardNumber = function() {
		var text = $("#IcardNum");
		var value = text.val();
		var flag = false;
		var errorMsg = "";
		if(!Helper.isEmpty(value)){
			flag = Helper.validata.isIdCardNo(value);
			if(!flag) {
				errorMsg = "身份证号码格式错误";
			}
		}else {
			errorMsg = "请输入身份证号码";
		}
		if(flag) {
			$(text).nextAll("span:last").html("").removeClass("red").addClass("gray");
		}else {
			$(text).nextAll("span:last").html("<b class='aqxx_bg gantan_ico mr_2'></b>"+errorMsg).removeClass("gray").addClass("red");
		}
		return flag;
	};
	/**
	 * 验证码
	 */
	var checkCodet = function() {
		var text = $("#IcheckCode");
		var checkCode = text.val();
		if(Helper.isEmpty(checkCode)) {
			$(text).nextAll("span:last").html("<b class='aqxx_bg gantan_ico mr_2'></b>"+"请输入验证码！").removeClass("gray").addClass("red");
			return false;
		}
		return true;
	};
	
	$("#IcardNum").blur(checkCardNumber).focus(function(){
		$(this).nextAll("span:last").html("请输入您的身份证号码").removeClass("red").addClass("gray");
	});
	$("#IcheckCode").blur(checkCodet).focus(function(){
		$(this).nextAll("span:last").html("").removeClass("red").addClass("gray");
	});
	Helper.doms.muberKeyupEmpty($("#IcheckCode")[0]);
	$("form")[0].reset();
	
	var setUsername = function(result){
		var showTexts = "";
		if(result != null && result.trim().length > 0) {
			var names = result.split(",");
			showTexts = "<span class='s_16 yellow'>通过该身份证查询到如下账户：</span>";
			showTexts += "<ul style='text-align:left;'>";
			for(var i = 0; i < names.length; i++) {
				if(names[i] != null && names[i].trim().length > 0) {
					if(i % 6 == 0)
					{
						showTexts += "<li>";
					}
					showTexts += "<span class='s_16  l' style='width:170px;'>"+"(";
					showTexts += (i+1);
					showTexts += ") ";
					showTexts += names[i]+"</span>";
					showTexts += "&nbsp;&nbsp;";
					if((i+1) % 6 == 0)
					{
						showTexts += "</li>";
					}
				}
			}
			showTexts += "</ul>";
			showTexts += "<div class='clr'></div>";
		}else {
			showTexts = "<span class='red'>对不起，没有对应用户信息！若有疑问，请联系客服！</span>";
		}
		Helper.message(showTexts);
	};
	
	var queryName = new Helper.RemoteTrans({
		url: Helper.basePath + "/doQueryName.htm",
		onSuccess: function(result){
			if("-1" == result) {
				Helper.message("验证码错误！");
				$("#query_img_a").click();
			}else {
				setUsername(result);
			}
		},
		onError: function(result, status){
			Helper.message(result.message);
		}
	});
	$("#query_img_a").click(function(){
		var newUrl = "";
		var oldUrl = $("#query_img").attr("src");
		if (oldUrl.indexOf("?") != -1) {
			newUrl = oldUrl.substring(0, oldUrl.indexOf("?")) + "?refresh="
					+ Math.random() * 100;
		} else {
			newUrl = oldUrl + "?refresh=" + Math.random() * 100;
		}
		$("#query_img").attr("src", newUrl);
	});
	$("#query_name_button").click(function(){
		if(checkCardNumber() && checkCodet()) {
			var value = $("#IcardNum").val();
			var checkCode = $("#IcheckCode").val();
			queryName.addParameters("cardNum", value);
			queryName.addParameters("checkCode", checkCode);
			queryName.send();
		}
	});
});